package com.gosling.cloudspace.tools;

import cn.hutool.core.collection.CollectionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * 集合工具类
 *
 * @author lihonglin
 * @date 2022/5/26 10:41
 */
public class CollectionTool extends CollectionUtil {
    protected static final Logger logger = LoggerFactory.getLogger(CollectionTool.class);

    /***
     * 求指定集合的并集
     *
     * @param set1
     * @param set2
     * @return
     */
    public static Set<String> unionSet(Set<String> set1, Set<String> set2) {
        Set<String> unionSet = new HashSet<>();
        unionSet.addAll(set1);
        unionSet.addAll(set2);
        logger.info("并集：{}", unionSet);
        return unionSet;
    }

    /***
     * 求指定集合的交集
     *
     * @param set1
     * @param set2
     * @return
     */
    public static Set<String> intersectionSet(Set<String> set1, Set<String> set2) {
        Set<String> intersectionSet = new HashSet<>(set1);
        intersectionSet.addAll(set1);
        intersectionSet.retainAll(set2);
        logger.info("交集：{}", intersectionSet);
        return intersectionSet;
    }

    /***
     * 求指定集合的差集
     *
     * @param set1
     * @param set2
     * @return
     */
    public static Set<String> differenceSet(Set<String> set1, Set<String> set2) {
        Set<String> differenceSet = new HashSet<>(set1);
        differenceSet.removeAll(set2);
        logger.info("差集：{}", differenceSet);
        return differenceSet;
    }

    public static LinkedHashSet<String> differenceSet2(LinkedHashSet<String> set1, LinkedHashSet<String> set2) {
        LinkedHashSet<String> differenceSet = new LinkedHashSet<>(set1);
        differenceSet.removeAll(set2);
        logger.info("差集：{}", differenceSet);
        return differenceSet;
    }

    public static void main(String[] args) {
        Set<String> set1 = new HashSet<String>() {{
            add("1");
            add("2");
        }};

        System.out.println(set1.toString());
//        Set<String> set2 = new HashSet<String>() {{
//            add("1");
//            add("3");
//        }};
//        differenceSet(set1, set2);
//        Set<String> set = intersectionSet(set1, set2);
//        for (String s : set) {
//            System.out.println(s);
//        }
//        unionSet(set1, set2);
    }
}
